package GUI;

import game.Character;
import game.GameBase;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class HUD {
	
	BufferedImage hud1, hud2, hud3, healthOrb, glassOrb, manaOrb, healthBarCover, healthBar, movesBar;
	Character pc = null;
	
	public HUD(){
		hud1 = GameBase.getImageByTag("hud1");
		hud2 = GameBase.getImageByTag("hud2");
		hud3 = GameBase.getImageByTag("hud3");
		healthOrb = GameBase.getImageByTag("health-orb");
		glassOrb = GameBase.getImageByTag("glass-orb");
		manaOrb = GameBase.getImageByTag("mana-orb");
		healthBarCover = GameBase.getImageByTag("bar-cover");
		healthBar = GameBase.getImageByTag("health-bar");
		movesBar = GameBase.getImageByTag("moves-bar");
		
		pc = Game.get().getPlayer();
	}
	
	public void draw(Graphics2D g, int scrW, int scrH)
	{
		g.drawImage(hud1, 0, scrH-hud1.getHeight(), null);
		g.drawImage(hud3, scrW-hud3.getWidth(), scrH-hud3.getHeight(), null);
		g.drawImage(hud2, hud1.getWidth(), scrH-hud2.getHeight(), scrW-hud3.getWidth(), scrH, 0, 0, 1, hud2.getHeight(), null);
		
		drawOrb(g, healthOrb, 0, scrH-healthOrb.getHeight(), pc.getMaxMana(), pc.getMana());
		drawOrb(g, manaOrb, scrW-manaOrb.getWidth(), scrH-manaOrb.getHeight(), pc.getMaxHealth(), pc.getHealth());
		
		ButtonSkin.drawText(g, "Health " + pc.getHealth() + " / " + pc.getMaxHealth(), Color.white,
				0, scrH-healthOrb.getHeight(),
				healthOrb.getWidth(), healthOrb.getHeight(),
				ButtonSkin.CENTER, true);
		ButtonSkin.drawText(g, "Mana " + pc.getMana() + " / " + pc.getMaxMana(), Color.white,
				scrW-manaOrb.getWidth(), scrH-manaOrb.getHeight(), 
				manaOrb.getWidth(), manaOrb.getHeight(),
				ButtonSkin.CENTER, true);
		
		drawBar(g, movesBar, healthBarCover, hud1.getWidth()+150, scrH-50, scrW-hud3.getWidth()-150, scrH-10, 
				pc.getMaxMovementPoints(), pc.getMovementPointsLeft());
		
		ButtonSkin.drawText(g, "Movement points " + pc.getMovementPointsLeft() + " / " + pc.getMaxMovementPoints(), Color.white,
				hud1.getWidth() + 150, scrH - 50, 
				scrW - hud3.getWidth() - hud1.getWidth() - 300, 40,
				ButtonSkin.CENTER, true);
	}
	
	private void drawOrb(Graphics2D g, BufferedImage img, int x, int y, int fullOrb, int nowOrb){
			g.drawImage(img, x, y+calcOrb(img, fullOrb, nowOrb), 
						x+img.getWidth(), y+img.getHeight(),
						0, calcOrb(img, fullOrb, nowOrb),
						img.getWidth(), img.getHeight(), null);
			g.drawImage(glassOrb, x, y, null);
	}
	public static void drawBarWH(Graphics2D g, BufferedImage img, BufferedImage cover, int x, int y, int w, int h, int fullBar, int nowBar)
	{
		drawBar(g, img, cover, x, y, x + w, y + h, fullBar, nowBar);
	}
	private static void drawBar(Graphics2D g, BufferedImage img, BufferedImage cover, int x, int y, int doX, int doY, int fullBar, int nowBar){
		float cW = (doX-x)/(float)img.getWidth();
		float cH = (doY-y)/(float)img.getHeight();
		g.drawImage(img, x, y, 
					x+(int)(cW*calcBar(img, fullBar, nowBar)), y+(int)(cH*img.getHeight()),
					0, 0, 
					calcBar(img, fullBar, nowBar), img.getHeight(), null);
		g.drawImage(cover, x, y, doX-x, doY-y, null);
	}
	private int calcOrb(BufferedImage img, int full, int now){
		float res = 1-(float)now/(float)full;
		res *= img.getHeight();
		return (int)res;
	}
	private static int calcBar(BufferedImage img, int full, int now){
		float res = (float)now/(float)full;
		res *= img.getWidth();
		return (int)res;
	}
}
